home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tk8.0 / win / tkWinKey.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-15  |  7.5 KB  |  361 lines  |  [TEXT/CWIE]

  1. /* 
  2.  * tkWinKey.c --
  3.  *
  4.  *    This file contains X emulation routines for keyboard related
  5.  *    functions.
  6.  *
  7.  * Copyright (c) 1995 Sun Microsystems, Inc.
  8.  *
  9.  * See the file "license.terms" for information on usage and redistribution
  10.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11.  *
  12.  * SCCS: @(#) tkWinKey.c 1.9 97/06/20 15:12:39
  13.  */
  14.  
  15. #include "tkWinInt.h"
  16.  
  17. typedef struct {
  18.     unsigned int keycode;
  19.     KeySym keysym;
  20. } Keys;
  21.  
  22. static Keys keymap[] = {
  23.     VK_CANCEL, XK_Cancel,
  24.     VK_BACK, XK_BackSpace,
  25.     VK_TAB, XK_Tab,
  26.     VK_CLEAR, XK_Clear,
  27.     VK_RETURN, XK_Return,
  28.     VK_SHIFT, XK_Shift_L,
  29.     VK_CONTROL, XK_Control_L,
  30.     VK_MENU, XK_Alt_L,
  31.     VK_PAUSE, XK_Pause,
  32.     VK_CAPITAL, XK_Caps_Lock,
  33.     VK_ESCAPE, XK_Escape,
  34.     VK_SPACE, XK_space,
  35.     VK_PRIOR, XK_Prior,
  36.     VK_NEXT, XK_Next,
  37.     VK_END, XK_End,
  38.     VK_HOME, XK_Home,
  39.     VK_LEFT, XK_Left,
  40.     VK_UP, XK_Up,
  41.     VK_RIGHT, XK_Right,
  42.     VK_DOWN, XK_Down,
  43.     VK_SELECT, XK_Select,
  44.     VK_PRINT, XK_Print,
  45.     VK_EXECUTE, XK_Execute,
  46.     VK_INSERT, XK_Insert,
  47.     VK_DELETE, XK_Delete,
  48.     VK_HELP, XK_Help,
  49.     VK_F1, XK_F1,
  50.     VK_F2, XK_F2,
  51.     VK_F3, XK_F3,
  52.     VK_F4, XK_F4,
  53.     VK_F5, XK_F5,
  54.     VK_F6, XK_F6,
  55.     VK_F7, XK_F7,
  56.     VK_F8, XK_F8,
  57.     VK_F9, XK_F9,
  58.     VK_F10, XK_F10,
  59.     VK_F11, XK_F11,
  60.     VK_F12, XK_F12,
  61.     VK_F13, XK_F13,
  62.     VK_F14, XK_F14,
  63.     VK_F15, XK_F15,
  64.     VK_F16, XK_F16,
  65.     VK_F17, XK_F17,
  66.     VK_F18, XK_F18,
  67.     VK_F19, XK_F19,
  68.     VK_F20, XK_F20,
  69.     VK_F21, XK_F21,
  70.     VK_F22, XK_F22,
  71.     VK_F23, XK_F23,
  72.     VK_F24, XK_F24,
  73.     VK_NUMLOCK, XK_Num_Lock, 
  74.     VK_SCROLL, XK_Scroll_Lock,
  75.     0, NoSymbol
  76. };
  77.  
  78.  
  79. /*
  80.  *----------------------------------------------------------------------
  81.  *
  82.  * XLookupString --
  83.  *
  84.  *    Retrieve the string equivalent for the given keyboard event.
  85.  *
  86.  * Results:
  87.  *    Returns the number of characters stored in buffer_return.
  88.  *
  89.  * Side effects:
  90.  *    Retrieves the characters stored in the event and inserts them
  91.  *    into buffer_return.
  92.  *
  93.  *----------------------------------------------------------------------
  94.  */
  95.  
  96. int
  97. XLookupString(event_struct, buffer_return, bytes_buffer, keysym_return,
  98.     status_in_out)
  99.     XKeyEvent* event_struct;
  100.     char* buffer_return;
  101.     int bytes_buffer;
  102.     KeySym* keysym_return;
  103.     XComposeStatus* status_in_out;
  104. {
  105.     int i, limit;
  106.  
  107.     if (event_struct->send_event != -1) {
  108.     /*
  109.      * This is an event generated from generic code.  It has no
  110.      * nchars or trans_chars members. 
  111.      */
  112.  
  113.     int index;
  114.     KeySym keysym;
  115.  
  116.     index = 0;
  117.     if (event_struct->state & ShiftMask) {
  118.         index |= 1;
  119.     }
  120.     if (event_struct->state & Mod1Mask) {
  121.         index |= 2;
  122.     }
  123.     keysym = XKeycodeToKeysym(event_struct->display, 
  124.         event_struct->keycode, index);
  125.     if (((keysym != NoSymbol) && (keysym > 0) && (keysym < 256)) 
  126.         || (keysym == XK_Return)
  127.         || (keysym == XK_Tab)) {
  128.         buffer_return[0] = (char) keysym;
  129.         return 1;
  130.     }
  131.     return 0;
  132.     }
  133.     if ((event_struct->nchars <= 0) || (buffer_return == NULL)) {
  134.     return 0;
  135.     }
  136.     limit = (event_struct->nchars < bytes_buffer) ? event_struct->nchars :
  137.     bytes_buffer;
  138.  
  139.     for (i = 0; i < limit; i++) {
  140.     buffer_return[i] = event_struct->trans_chars[i];
  141.     }
  142.  
  143.     if (keysym_return != NULL) {
  144.     *keysym_return = NoSymbol;
  145.     }
  146.     return i;
  147. }
  148.  
  149. /*
  150.  *----------------------------------------------------------------------
  151.  *
  152.  * XKeycodeToKeysym --
  153.  *
  154.  *    Translate from a system-dependent keycode to a
  155.  *    system-independent keysym.
  156.  *
  157.  * Results:
  158.  *    Returns the translated keysym, or NoSymbol on failure.
  159.  *
  160.  * Side effects:
  161.  *    None.
  162.  *
  163.  *----------------------------------------------------------------------
  164.  */
  165.  
  166. KeySym
  167. XKeycodeToKeysym(display, keycode, index)
  168.     Display* display;
  169.     unsigned int keycode;
  170.     int index;
  171. {
  172.     Keys* key;
  173.     BYTE keys[256];
  174.     int result;
  175.     char buf[4];
  176.     unsigned int scancode = MapVirtualKey(keycode, 0);
  177.  
  178.     memset(keys, 0, 256);
  179.     if (index & 0x02) {
  180.     keys[VK_NUMLOCK] = 1;
  181.     }
  182.     if (index & 0x01) {
  183.     keys[VK_SHIFT] = 0x80;
  184.     }
  185.     result = ToAscii(keycode, scancode, keys, (LPWORD) buf, 0);
  186.  
  187.     /*
  188.      * Keycode mapped to a valid Latin-1 character.  Since the keysyms
  189.      * for alphanumeric characters map onto Latin-1, we just return it.
  190.      */
  191.  
  192.     if (result == 1 && buf[0] >= 0x20) {
  193.     return (KeySym) buf[0];
  194.     }
  195.  
  196.     /*
  197.      * Keycode is a non-alphanumeric key, so we have to do the lookup.
  198.      */
  199.  
  200.     for (key = keymap; key->keycode != 0; key++) {
  201.     if (key->keycode == keycode) {
  202.         return key->keysym;
  203.     }
  204.     }
  205.  
  206.     return NoSymbol;
  207. }
  208.  
  209. /*
  210.  *----------------------------------------------------------------------
  211.  *
  212.  * XKeysymToKeycode --
  213.  *
  214.  *    Translate a keysym back into a keycode.
  215.  *
  216.  * Results:
  217.  *    Returns the keycode that would generate the specified keysym.
  218.  *
  219.  * Side effects:
  220.  *    None.
  221.  *
  222.  *----------------------------------------------------------------------
  223.  */
  224.  
  225. KeyCode
  226. XKeysymToKeycode(display, keysym)
  227.     Display* display;
  228.     KeySym keysym;
  229. {
  230.     Keys* key;
  231.     SHORT result;
  232.  
  233.     if (keysym >= 0x20) {
  234.     result = VkKeyScan((char) keysym);
  235.     if (result != -1) {
  236.         return (KeyCode) (result & 0xff);
  237.     }
  238.     }
  239.  
  240.     /*
  241.      * Couldn't map the character to a virtual keycode, so do a
  242.      * table lookup.
  243.      */
  244.  
  245.     for (key = keymap; key->keycode != 0; key++) {
  246.     if (key->keysym == keysym) {
  247.         return key->keycode;
  248.     }
  249.     }
  250.     return 0;
  251. }
  252.  
  253. /*
  254.  *----------------------------------------------------------------------
  255.  *
  256.  * XGetModifierMapping --
  257.  *
  258.  *    Fetch the current keycodes used as modifiers.
  259.  *
  260.  * Results:
  261.  *    Returns a new modifier map.
  262.  *
  263.  * Side effects:
  264.  *    Allocates a new modifier map data structure.
  265.  *
  266.  *----------------------------------------------------------------------
  267.  */
  268.  
  269. XModifierKeymap    *
  270. XGetModifierMapping(display)
  271.     Display* display;
  272. {
  273.     XModifierKeymap *map = (XModifierKeymap *)ckalloc(sizeof(XModifierKeymap));
  274.  
  275.     map->max_keypermod = 1;
  276.     map->modifiermap = (KeyCode *) ckalloc(sizeof(KeyCode)*8);
  277.     map->modifiermap[ShiftMapIndex] = VK_SHIFT;
  278.     map->modifiermap[LockMapIndex] = VK_CAPITAL;
  279.     map->modifiermap[ControlMapIndex] = VK_CONTROL;
  280.     map->modifiermap[Mod1MapIndex] = VK_NUMLOCK;
  281.     map->modifiermap[Mod2MapIndex] = VK_MENU;
  282.     map->modifiermap[Mod3MapIndex] = VK_SCROLL;
  283.     map->modifiermap[Mod4MapIndex] = 0;
  284.     map->modifiermap[Mod5MapIndex] = 0;
  285.     return map;
  286. }
  287.  
  288. /*
  289.  *----------------------------------------------------------------------
  290.  *
  291.  * XFreeModifiermap --
  292.  *
  293.  *    Deallocate a modifier map that was created by
  294.  *    XGetModifierMapping.
  295.  *
  296.  * Results:
  297.  *    None.
  298.  *
  299.  * Side effects:
  300.  *    Frees the datastructure referenced by modmap.
  301.  *
  302.  *----------------------------------------------------------------------
  303.  */
  304.  
  305. void
  306. XFreeModifiermap(modmap)
  307.     XModifierKeymap* modmap;
  308. {
  309.     ckfree((char *) modmap->modifiermap);
  310.     ckfree((char *) modmap);
  311. }
  312.  
  313. /*
  314.  *----------------------------------------------------------------------
  315.  *
  316.  * XStringToKeysym --
  317.  *
  318.  *    Translate a keysym name to the matching keysym. 
  319.  *
  320.  * Results:
  321.  *    Returns the keysym.  Since this is already handled by
  322.  *    Tk's StringToKeysym function, we just return NoSymbol.
  323.  *
  324.  * Side effects:
  325.  *    None.
  326.  *
  327.  *----------------------------------------------------------------------
  328.  */
  329.  
  330. KeySym
  331. XStringToKeysym(string)
  332.     _Xconst char *string;
  333. {
  334.     return NoSymbol;
  335. }
  336.  
  337. /*
  338.  *----------------------------------------------------------------------
  339.  *
  340.  * XKeysymToString --
  341.  *
  342.  *    Convert a keysym to character form.
  343.  *
  344.  * Results:
  345.  *    Returns NULL, since Tk will have handled this already.
  346.  *
  347.  * Side effects:
  348.  *    None.
  349.  *
  350.  *----------------------------------------------------------------------
  351.  */
  352.  
  353. char *
  354. XKeysymToString(keysym)
  355.     KeySym keysym;
  356. {
  357.     return NULL;
  358. }
  359.  
  360.  
  361.